<?php

namespace App\Http\Controllers;

use App\Models\Master;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function index()
    {
        return view('admin.index');
    }
    public function login()
    {
        return view('admin.login');
    }
    public function dologin(Request $request){//登录处理逻辑
        $name=$request->name;
        $password=$request->password;
        if(Auth::attempt(['name'=>$name,'password'=>$password])){
            return json_encode(['success'=>true,'msg'=>'用户成功登录']);
        }
        else{
            return json_encode(['success'=>false,'msg'=>'用户名或密码错误请重新登录']);
        }
    }
    public function create(){//新增管理员
        return view('admin.user_create');
    }

    public function store(Request $request){//新增处理逻辑
        $aUser=$request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，横线或下划线，不能包含其他字符',
            'name.min'=>'用户名至少要有:min个字符',
            'name.unique'=>'用户名已存在',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不合法',
            'email.unique'=>'邮箱已存在',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码至少要有:min个字符',
            'password.confirmed'=>'两次输入密码不一致',
        ];
        $validate=Validator::make($aUser,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $aUser['password']=Hash::make($aUser['password']);
        User::create($aUser);
        return json_encode(['success'=>true,'msg'=>'成功']);
    }
    public function edit($id){//修改页面
        $oUser=User::find($id);
        return view('admin.user_edit')->with('oUser',$oUser);
    }


    public function update(Request $request,$id){//修改处理逻辑
        $user=User::find($id);
        $aUser=$request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|:users',

            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，横线或下划线，不能包含其他字符',
            'name.min'=>'用户名至少要有:min个字符',


            'password.required'=>'密码不能为空',
            'password.min'=>'密码至少要有:min个字符',
            'password.confirmed'=>'两次输入密码不一致',
        ];
        $validate=Validator::make($aUser,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $aUser['password']=Hash::make($aUser['password']);
        $user->name=$aUser['name'];
        $user->password=$aUser['password'];
        $user->save();
        return json_encode(['success'=>true,'msg'=>'成功']);
    }

    public function destroy($id){//删除
        $oUser=User::find($id);
        $oUser->delete();
        return redirect('/adm/list');
    }

        public function list()
    {
        $oUsers=User::paginate(3);
        return view('admin.list')->with('oUsers',$oUsers);
    }

    public function logout(){//退出登录
        Auth::logout();
        return redirect('/login');
    }

    public function form()
    {
        $oMasters=Master::paginate(5);//分页
        return view('admin.form')->with('oMasters',$oMasters);
    }
}
